package com.v1.video.util;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Video.Thumbnails;
import android.widget.ImageView;

public class LocalImageAsk  
{  
    /** 
     * 内存图片软引用缓冲 
     */  
    private HashMap<String, SoftReference<Bitmap>> imageCache = null;  
      
    public LocalImageAsk()  
    {  
        imageCache = new HashMap<String, SoftReference<Bitmap>>();  
    }  
      
    public Bitmap loadBitmap(final ImageView imageView, final String imageURL, final ImageCallBack imageCallBack)  
    {  
        //在内存缓存中，则返回Bitmap对象  
        if(imageCache.containsKey(imageURL))  
        {  
            SoftReference<Bitmap> reference = imageCache.get(imageURL);  
            Bitmap bitmap = reference.get();  
            if(bitmap != null)  
            {  
                return bitmap;  
            }  
        }  
          
        final Handler handler = new Handler()  
        {  
            /* (non-Javadoc) 
             * @see android.os.Handler#handleMessage(android.os.Message) 
             */  
            @Override  
            public void handleMessage(Message msg)  
            {  
                // TODO Auto-generated method stub  
                imageCallBack.imageLoad(imageView, (Bitmap)msg.obj);  
            }  
        };  
          
        //如果不在内存缓存中，也不在本地（被jvm回收掉），则开启线程下载图片  
        new Thread()  
        {  
            /* (non-Javadoc) 
             * @see java.lang.Thread#run() 
             */  
            @Override  
            public void run()  
            {
                // TODO Auto-generated method stub  
                try{
                    Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(imageURL, Thumbnails.MICRO_KIND); 
                    imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));  
                    Message msg = handler.obtainMessage(0, bitmap);  
                    handler.sendMessage(msg);                    
                }catch(Exception e){
                    e.printStackTrace();
                }
            }  
        }.start();  
          
        return null;  
    }  
    
    public Bitmap loadImageTask(final ImageView imageView,final ArrayList<String> dataList,final ImageCallBack imageCallBack){    	        
          
        final Handler handler = new Handler()  
        {  
            /* (non-Javadoc) 
             * @see android.os.Handler#handleMessage(android.os.Message) 
             */  
            @Override  
            public void handleMessage(Message msg)  
            {  
                // TODO Auto-generated method stub  
                imageCallBack.imageLoad(imageView, (Bitmap)msg.obj);  
            }  
        };  
          
        //如果不在内存缓存中，也不在本地（被jvm回收掉），则开启线程下载图片  
        new Thread()  
        {  
            /* (non-Javadoc) 
             * @see java.lang.Thread#run() 
             */  
            @Override  
            public void run()  
            {
                // TODO Auto-generated method stub  
                try{
                	for(int i = 0; i < dataList.size();i++){
                		String imageURL = dataList.get(i);
                		Bitmap bitmap = null;
                		//在内存缓存中，则返回Bitmap对象  
                        if(imageCache.containsKey(imageURL))  
                        {  
                            SoftReference<Bitmap> reference = imageCache.get(imageURL);  
                            bitmap = reference.get();  
                            
                        }else{
		                    bitmap = ThumbnailUtils.createVideoThumbnail(imageURL, Thumbnails.MICRO_KIND); 
		                    imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));
	                    }  
	                    Message msg = handler.obtainMessage(0, bitmap);  
	                    handler.sendMessage(msg);               
                    }     
                }catch(Exception e){
                    e.printStackTrace();
                }
            }  
        }.start();  
          
        return null;      
    }

    public interface ImageCallBack  
    {  
        public void imageLoad(ImageView imageView, Bitmap bitmap);  
    }  
    
    public void clear(){
        if(imageCache != null){
            imageCache.clear();
        }
    }
}
